<%@ page contentType="text/html;charset=UTF-8" %>
<script>
	    var $subjectTreeTable=null;  
		$(document).ready(function() {
			$subjectTreeTable=$('#subjectTreeTable').treeTable({  
		    	   theme:'vsStyle',	           
					expandLevel : 2,
					column:0,
					checkbox: false,
		            url:'${ctx}/ap/subject/getChildren?parentId=',  
		            callback:function(item) { 
		            	 var treeTableTpl= $("#subjectTreeTableTpl").html();

		            	 var result = laytpl(treeTableTpl).render({
								  row: item
							});
		                return result;                   
		            },  
		            beforeClick: function($subjectTreeTable, id) { 
		                //异步获取数据 这里模拟替换处理  
		                $subjectTreeTable.refreshPoint(id);  
		            },  
		            beforeExpand : function($subjectTreeTable, id) {   
		            },  
		            afterExpand : function($subjectTreeTable, id) {  
		            },  
		            beforeClose : function($subjectTreeTable, id) {    
		            	
		            }  
		        });
		        
		        $subjectTreeTable.initParents('${parentIds}', "0");//在保存编辑时定位展开当前节点
            $("#btnImport").click(function(){
                jp.open({
                    type: 1,
                    area: [500, 300],
                    title:"导入数据",
                    content:$("#importBox").html() ,
                    btn: ['下载模板','确定', '关闭'],
                    btn1: function(index, layero){
                        window.location='${ctx}/ap/subject/import/template';
                    },
                    btn2: function(index, layero){
                        var inputForm =top.$("#importForm");
                        var top_iframe = top.getActiveTab().attr("name");//获取当前active的tab的iframe
                        inputForm.attr("target",top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示
                        inputForm.onsubmit = function(){
                            jp.loading('  正在导入，请稍等...');
                        }
                        inputForm.submit();
                        jp.close(index);
                    },

                    btn3: function(index){
                        jp.close(index);
                    }
                });
            });
		});
		
		function del(con,id){  
			jp.confirm('确认要删除科目吗？', function(){
				jp.loading();
	       	  	$.get("${ctx}/ap/subject/delete?id="+id, function(data){
	       	  		if(data.success){
	       	  			$subjectTreeTable.del(id);
	       	  			jp.success(data.msg);
	       	  		}else{
	       	  			jp.error(data.msg);
	       	  		}
	       	  	})
	       	   
       		});
	
		} 
		
		function refresh(){//刷新
			var index = jp.loading("正在加载，请稍等...");
			$subjectTreeTable.refresh();
			jp.close(index);
		}
		function dc() {
            window.location="${ctx}/ap/subject/export";
        }
</script>
<script type="text/html" id="subjectTreeTableTpl">
			<td><a  href="#" onclick="jp.openDialogView('查看科目', '${ctx}/ap/subject/form?id={{d.row.id}}','800px', '500px')">
				{{d.row.name === undefined ? "": d.row.name}}
			</a></td>
			<td>
				{{d.row.type === undefined ? "": d.row.type}}
			</td>
			<td>
				{{d.row.remarks === undefined ? "": d.row.remarks}}
			</td>
			<td>
				<div class="btn-group">
			 		<button type="button" class="btn  btn-primary btn-xs dropdown-toggle" data-toggle="dropdown">
						<i class="fa fa-cog"></i>
						<span class="fa fa-chevron-down"></span>
					</button>
				  <ul class="dropdown-menu" role="menu">
					<shiro:hasPermission name="ap:subject:view">
						<li><a href="#" onclick="jp.openDialogView('查看科目', '${ctx}/ap/subject/form?id={{d.row.id}}','800px', '500px')"><i class="fa fa-search-plus"></i> 查看</a></li>
					</shiro:hasPermission>
					<shiro:hasPermission name="ap:subject:edit">
						<li><a href="#" onclick="jp.openDialog('修改科目', '${ctx}/ap/subject/form?id={{d.row.id}}','800px', '500px', $subjectTreeTable)"><i class="fa fa-edit"></i> 修改</a></li>
		   			</shiro:hasPermission>
		   			<shiro:hasPermission name="ap:subject:del">
		   				<li><a  onclick="return del(this, '{{d.row.id}}')"><i class="fa fa-trash"></i> 删除</a></li>
					</shiro:hasPermission>
		   			<shiro:hasPermission name="ap:subject:add">
						<li><a href="#" onclick="jp.openDialog('添加下级科目', '${ctx}/ap/subject/form?parent.id={{d.row.id}}','800px', '500px', $subjectTreeTable)"><i class="fa fa-plus"></i> 添加下级科目</a></li>
					</shiro:hasPermission>
				  </ul>
				</div>
			</td>
	</script>